﻿using System;
using System.Web;
using System.Web.Security;
using good1.ApplicationServices.Interface;

namespace good1.Web.Controllers.Infrastructure.Cookies
{
    /// <summary>
    /// Using cookies to store non-sensitive data such as name, preferences and settings.
    /// </summary>
    public class GoodCookies : IGoodCookieService
    {
        private readonly int howManyDaysToKeep;
        private bool insideHttpPostEvent;

        private readonly ICookieContainer cookieContainer;

        public GoodCookies(ICookieContainer cookieContainer)
        {
            this.cookieContainer = cookieContainer;
            howManyDaysToKeep = 1000;
            insideHttpPostEvent = false;
        }

        public string UserEmail
        {
            get { return cookieContainer.GetValue("UserEmail"); }
            set { cookieContainer.SetValue("UserEmail", value, DateTime.Now.AddDays(howManyDaysToKeep), insideHttpPostEvent); }
        }

        public bool InsideHttpPostEvent
        {
            get { return insideHttpPostEvent; }
            set { insideHttpPostEvent = value; }
        }



        public void AddCookie(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name, value);
            cookieContainer.AddCookie(cookie);
        }

        public void AddAuthenticationCookie(string username, bool rememberMe, string roles)
        {
            //FormsAuthentication.SetAuthCookie(loginForm.Email, loginForm.RememberMe);
            //above would be a simple way to add auth to cookie but does not allow for the roles to be stored as below. 

            FormsAuthenticationTicket authTicket =
              new FormsAuthenticationTicket(1,
                                            username,
                                            DateTime.Now,
                                            DateTime.Now.AddMinutes(60),
                                            rememberMe,
                                            roles);
            string encTicket = FormsAuthentication.Encrypt(authTicket);
            AddCookie(FormsAuthentication.FormsCookieName, encTicket);
        }

        public string AuthenticatedName
        {
            get
            {
                return AuthenticationTicket == null ? "" : AuthenticationTicket.Name;
            }
        }

        public string[] AuthenticatedRoles
        {
            get
            {
                string[] roles = AuthenticationTicket.UserData.Split(new Char[] { '|' });
                return roles;
            }
        }

        private static FormsAuthenticationTicket AuthenticationTicket
        {
            get
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (authCookie == null) return null;
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                return authTicket;
            }

        }

        public int? UserId
        {
            get { return cookieContainer.GetValue<int?>("UserId"); }
            set { cookieContainer.SetValue("UserId", value, DateTime.Now.AddDays(howManyDaysToKeep), insideHttpPostEvent); }
        }

        public string FriendlyName
        {
            get { return cookieContainer.GetValue("FriendlyName"); }
            set { cookieContainer.SetValue("FriendlyName", value, DateTime.Now.AddDays(howManyDaysToKeep), insideHttpPostEvent); }
        }

        public DateTime? LastVisit
        {
            get { return cookieContainer.GetValue<DateTime?>("LastVisit"); }
            set { cookieContainer.SetValue("LastVisit", value, DateTime.Now.AddDays(howManyDaysToKeep), insideHttpPostEvent); }
        }



    }
}
